package com.maker.LMMS.service;

import com.blade.jdbc.page.Page;
import com.blade.mvc.http.Request;
import com.maker.LMMS.model.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : zimu
 * @Date: 2018/8/3 12:07
 * @description : 显示信息实现类
 */
public class ShowImpl {
    public void showIndex(Request request){
        Users users = new Users();
        Group group = new Group();
        List<Users>  usersList = users.queryAll("SELECT m_id,m_realname,m_group,m_head_portrait" +
                ",m_student_number FROM member WHERE m_flag=1 ORDER BY RAND() LIMIT 6");
        List groups = new ArrayList();
        for (Users user : usersList) {
            groups.add(group.queryAll("SELECT * FROM `group` WHERE g_id=?",user.getM_group()));
        }
        request.attribute("groups", groups);
        request.attribute("users", usersList);
    }

    public void showAllInformation(Request request){
        Users users = new Users();
        Group group = new Group();
        List<Users>  usersList = users.queryAll("SELECT * FROM member ORDER BY RAND() LIMIT 5 ");
        List<Group> lists=group.queryAll("SELECT * FROM `group`");
        Page<Users> pages=users.page(1,20);
        List groups = new ArrayList();
        for (Users user : usersList) {
            groups.add(group.queryAll("SELECT * FROM `group` WHERE g_id=?",user.getM_group()));
        }
        List<Users> userslist = users.findAll();
        request.attribute("groupsname",lists);
        request.attribute("memberInfo", userslist);
        request.attribute("groups", groups);
        request.attribute("users", usersList);
        request.attribute("page",pages);

    }

    public void showSomeNewMember(Request request) {
        Users users = new Users();
        Group group = new Group();
        int year = Integer.parseInt((""+new Date()).substring((""+new Date()).length()-4))-1;
        List<Users> usersList = users.queryAll("SELECT m_id,m_head_portrait,m_realname,m_group" +
                " FROM member WHERE m_joindate LIKE '%"+year+"%' AND m_flag=1  ORDER BY RAND() LIMIT 5 ");
        List groups = new ArrayList();
        for (Users user : usersList) {
            groups.add(group(user.getM_group()));
        }
        request.attribute("showGroups", groups);
        request.attribute("someNewMember", usersList);
    }

    public void showHeadMan(Request request){
        Users users = new Users();
        Group group = new Group();
        List<Group> groupList = group.queryAll("SELECT * FROM `group`");
        List<Group> groupLists= group.queryAll("SELECT DISTINCT(`g_gname`) FROM `group`");
        List userList = new ArrayList();
        for (Group group1 : groupList) {
            userList.add(users.queryAll("SELECT m_id," +
                    "m_realname, m_head_portrait FROM `member` WHERE m_id=? AND m_flag=1 " ,group1.getG_leader()));
        }
        request.attribute("headManList", groupList);
        request.attribute("headList",groupLists);
        request.attribute("headManNameList", userList);
    }

    public void showMember(Request request, Integer id){
        int year = Integer.parseInt((""+new Date()).substring((""+new Date()).length()-4));
        Users users = new Users();
        Group group = new Group();
        List<Group> lists=group.queryAll("SELECT `g_gname` FROM `group` WHERE g_id="+id+";");
        List<Users> usersMember=users.queryAll("SELECT * FROM `member` WHERE m_group IN (SELECT `g_id` FROM `group` WHERE g_id= "+
        id+") and m_flag=1");
        List groupName = new ArrayList();
        for (Users user : usersMember) {
            groupName.add(group.queryAll("SELECT * FROM `group` WHERE g_id=?",user.getM_group()));
        }
        List<Users> userslist = users.findAll();
        int pageNow=Integer.parseInt(request.query("pageName","1"));
        Page<Users> Number=users.like("m_id",id).page(pageNow,5);
        request.attribute("result",Number);
        request.attribute("groupsnames",lists);
        request.attribute("memberInfo", userslist);
        request.attribute("usersMember",usersMember);
        request.attribute("groupName",groupName);
        request.attribute("gid",id);
        request.attribute("year",year);
    }



    public void showinfo(Request request, Integer id) {
        Users users = new Users();
        users.setM_id(id);
        List<Users> usersList = users.findAll();
        usersList.get(0).setM_password("");
        if (request.session().attribute("name") == null) {
            usersList.get(0).setM_birthday("登录后可查看");
//            usersList.get(0).setM_qq("登录后可查看");
            usersList.get(0).setM_phone("登录后可查看");
            usersList.get(0).setM_email("登录后可查看");
            usersList.get(0).setM_birthday("登录后可查看");
            usersList.get(0).setM_trace("登录后可查看");
        }
        int graduate = 0;//未毕业
        int graduatey = Integer.parseInt(usersList.get(0).getM_studentNumber().substring(0,4));
        Date date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        int date1 = Integer.parseInt(sdf.format(date));
        int date2 = Integer.parseInt(new Date().getMonth()+1 + "");
        if(date1-graduatey>4||(date1-graduatey==4 && date2>6))
            graduate=1;//已毕业
        List<Group>groups = group(usersList.get(0).getM_group());
        request.attribute("group", groups);
        request.attribute("memberInfo", usersList);
        request.attribute("graduate", graduate);
    }

    public List<Group> group(Integer id) {
        Group group = new Group();
        return group.queryAll("SELECT * FROM `group` WHERE g_id=?", id);
    }


    public void showResult(Request request, String key, int pageNo){
        Users users = new Users();
        Group group = new Group();
        List groups = new ArrayList();
        Page<Users> searchNumber = users.like("m_realname","%"+key+"%").page(pageNo,7);
        for(Users user : searchNumber.getRows()){
            user.setM_password("");
        }
        for (Users user : searchNumber.getRows()) {
            groups.add(group.queryAll("SELECT * FROM `group` WHERE g_id=?",user.getM_group()));
        }
        request.attribute("result",searchNumber);
        request.attribute("groupResult", groups);
        request.attribute("key", key);
    }

    public void showMembers(Request request){
        int year = Integer.parseInt((""+new Date()).substring((""+new Date()).length()-4));
        Users users=new Users();
        List groups = new ArrayList();
        int pageNow=Integer.parseInt(request.query("pageNow","1"));
        Page<Users> Number=users.like("m_flag",1).page(pageNow,12,"m_id");
        for (Users user : Number.getRows()) {
            user.setM_password("");
            Group group = new Group();
            group.setG_id(user.getM_group());
            groups.add(group.find());
        }
        request.attribute("result",Number);
        request.attribute("groups1",groups);
        request.attribute("year",year);
    }

    public  void Pagegination(Request request,int pageNow){
        Users users=new Users();
        Page<Users> searchNumber = users.page(pageNow,1);
        request.attribute("result",searchNumber);
    }

    public void showSkill(Request request, Integer id) {
        Skills skills = new Skills();
        skills.setMemberid(id);
        List<Skills> skillsList = skills.findAll();
        if (skillsList.size()==0) {
            skillsList.add(skills);
        }
        request.attribute("skillsList", skillsList);
    }


    public void showExperience(Request request, Integer id) {
        Experience experience = new Experience();
        List<Experience> list = experience.queryAll("SELECT * FROM experience WHERE memberId=? ORDER BY id DESC", id);
        request.attribute("experience", list);
    }

    public void showAboutus(Request request){
        Aboutus aboutus = new Aboutus();
        aboutus = aboutus.query("SELECT * FROM `aboutus` where ab_id=1");
        request.attribute("aboutus", aboutus);
    }



//    展示发展历史
    public void showDevlopment(Request request){
        Development development=new Development();
        List<Development> developmentList=development.queryAll("SELECT * FROM `development` ORDER BY d_time DESC");
        if(developmentList.size()==0){
            developmentList.add(development);
        }
        //获取任务发布的时间
        Date date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = sdf.format(date);
        request.attribute("devList",developmentList);
        request.attribute("date",date1);
    }

    /**
     * 完善资料·信息显示
     * @param request
     */
    public void showWanshan(Request request, Integer id) {
        Users users = new Users();
        users.setM_id(id);
        users = users.find();
        users.setM_password("");
        Group mygroup = new Group();
        mygroup.setG_id(users.getM_group());
        mygroup = mygroup.find();
        Skills skills = new Skills();
        skills.setMemberid(id);
        skills = skills.find();
        if (skills==null){
            Skills skills1 = new Skills();
            skills1.setMemberid(id);
            skills1.save();
            skills = skills1.find();
        }
        if (mygroup==null){
            mygroup = new Group();
            mygroup.setG_gname("null");
        }
        Group group = new Group();
        List<Group> groups = group.findAll();
        request.attribute("member", users);
        request.attribute("mygroup", mygroup);
        request.attribute("groups", groups);
        request.attribute("skill", skills);
    }

    public boolean checkUser(Request request,Integer id) {
        Users users = new Users();
        users.setM_id(id);
        users = users.find();
        Users user = request.session().attribute("name");
        if (users.getM_flag() == 1) {
            if (user!=null && (user.getM_realname().equals(users.getM_realname()))){
                return true;
            }
            return false;
        }
        return true;
    }

    /**
     * 各组现状（想吐系列）
     * @param request
     */
    public void showGroup(Request request) {
        Group group = new Group();
        List members = new ArrayList();
        List leaders = new ArrayList();
        List gleader = new ArrayList();
        List leadersname = new ArrayList();
        List<Group> groups = group.findAll();
        for (Group group1 : groups){
            Users users = new Users();
            users.setM_group(group1.getG_id());
            List<Users> users1 = users.findAll();
            for (Users u : users1)
                u.setM_password("");
            members.add(users1);

            Users uleader = new Users();
            uleader.setM_id(group1.getG_leader());
            uleader = uleader.find();
            uleader.setM_password("");
            gleader.add(uleader);

            Leader leader = new Leader();
            List<Leader> leaders1 = leader.queryAll("select * from `leader` where le_groupid=? order by le_time",group1.getG_id());
            leaders.add(leaders1);
            List name = new ArrayList();
            for (Leader leader1 : leaders1){
                Users users2 = new Users();
                users2.setM_id(leader1.getLe_memberid());
                users2 = users2.find();
                users2.setM_password("");
                name.add(users2);
            }
            leadersname.add(name);
        }
        int year = Integer.parseInt((""+new Date()).substring((""+new Date()).length()-4))-1;//当前年份-1
        request.attribute("members", members);
        request.attribute("leaders", leaders);
        request.attribute("gleader", gleader);
        request.attribute("leadersname", leadersname);
        request.attribute("groups", groups);
        request.attribute("year", year);
    }

    public void showRegister(Request request) {
        Register register = new Register();
        register.setR_id(1);
        register = register.find();
        request.attribute("register", register);
    }

    public void showNews(Request request) {
        LeaveWord leaveWord = new LeaveWord();
        List users = new ArrayList();
        Users user = request.session().attribute("name");
        leaveWord.setL_accepter(user.getM_id());
        List<LeaveWord> allleaveWords = leaveWord.findAll();
        List<LeaveWord> leaveWords = leaveWord.queryAll("select * from `leave_word` where l_read=0 and l_accepter=? order by l_time desc",user.getM_id());
        for (LeaveWord leaveWord1 : leaveWords){
            leaveWord1.setL_content(Trunhtml.trunhtml(leaveWord1.getL_content()));
            Users users1 = new Users();
            users1.setM_id(leaveWord1.getL_sender());
            users1 = users1.find();
            users1.setM_password("");
            users.add(users1);
        }
        request.attribute("news", leaveWords);
        request.attribute("allleaveWords", allleaveWords);
        request.attribute("news_users", users);
    }

    public void showLeaveword(Request request, Integer id) {
        LeaveWord leaveWord = new LeaveWord();
        List lw_user = new ArrayList();
        int page=Integer.parseInt(request.query("page","1"));
        int read=Integer.parseInt(request.query("read","0"));
        Page<LeaveWord> leaveWordPage = leaveWord.like("l_accepter",id).page(page,5,"l_time desc");
        for (LeaveWord leaveWord1 : leaveWordPage.getRows()){
            if ((read==1||page>1)&&request.session().attribute("name")!=null){
                Users name = request.session().attribute("name");
                if (name.getM_id()==id) {
                    LeaveWord leaveWord2 = new LeaveWord();
                    leaveWord2.setL_read(1);
                    leaveWord2.update("l_id", leaveWord1.getL_id());
                }
            }
            Users users = new Users();
            users.setM_id(leaveWord1.getL_sender());
            lw_user.add(users.find());
            users.setM_password("");
        }
        request.attribute("leaveWordPage", leaveWordPage);
        request.attribute("lw_user", lw_user);
        request.attribute("page", page);
    }

    /**
     * 各届成员列表
     * @param request
     * @param id 第几届
     */
    public void showPeriodMembers(Request request, Integer id) {
        int year = Integer.parseInt((""+new Date()).substring((""+new Date()).length()-4));//当前年份
        int joinyear = 2015 +id;
        Users users = new Users();
        Group group = new Group();
        List<Users> usersMember=users.queryAll("SELECT * FROM `member` WHERE m_joindate like '"+joinyear+"%' or m_joindate like '"+(joinyear-1)+"%' and m_flag=1");
        if(id==0)
            usersMember = users.findAll();
        List groupName = new ArrayList();
        int i =0;
        for (Users user : usersMember) {
            groupName.add(group.queryAll("SELECT * FROM `group` WHERE g_id=?",user.getM_group()));
            if (id==0&&Integer.parseInt(user.getM_joindate().substring(0,4))>2014)
                if (!(Integer.parseInt(user.getM_joindate().substring(0,4))==2015&&user.getM_joindate().substring(5,6).equals("0")))
                    usersMember.get(i).setM_joindate("2000-01-01");
            i++;
        }
        request.attribute("usersMember",usersMember);
        request.attribute("groupName",groupName);
        request.attribute("pid",id);
        request.attribute("year",year);
        request.attribute("joinyear",joinyear+"");
        request.attribute("joinyear_1",(joinyear-1)+"");
    }
}
